home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / database / postgres / postgre4.z / postgre4 / src / test / testtrace.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-27  |  4.4 KB  |  174 lines

  1.  
  2. /* $Header: /private/postgres/src/test/RCS/testtrace.c,v 1.7 1989/09/05 16:55:02 mao Version_2 $
  3.  *   TESTTRACE.C
  4.  *
  5.  *   This program tests the trace module.  The trace module allows you
  6.  *   to turn on and off tracing according to one or more 'trace variables'.
  7.  *   Currently you can do two things with these variables:
  8.  *
  9.  *   (1) Call TraceMsg[0..2]() to print trace (debug) messages.  Whether
  10.  *     they are actually printed or not depends on whether the variables
  11.  *     are enabled or not.
  12.  *
  13.  *   (2) Track the subroutine call stack using TracePush() and TracePop().
  14.  *     All such calls are saved but only those whos associated trace
  15.  *     variables are enabled are actually printed.
  16.  *
  17.  *     A failed assertion or other exception will cause the stack
  18.  *     maintained by TracePush() and TracePop() align.  Additionaly,
  19.  *     some amount of error checking is done to attempt to catch 
  20.  *     programmers who forget to TracePop() what they have pushed.
  21.  *
  22.  *   To Use, setenv TRACE_VarName  <somenumber> for any or all the trace
  23.  *   variables and then run testtrace.  Example:
  24.  *
  25.  *   % setenv TRACE_Alpha 1
  26.  *   % setenv TRACE_Beta 1
  27.  *   % testtrace
  28.  *      ...
  29.  *   % setenv TESTASSERT 2
  30.  *   % testtrace
  31.  *
  32.  *   The TESTASSERT enviroment variable will cause an Assert(0) to be called
  33.  *   within the subroutine tracing test, forcing a program abort.  Before
  34.  *   the program aborts a trace dump will be printed as of that point.
  35.  */
  36.  
  37. #include <stdio.h>
  38. #include "trace.h"
  39.  
  40. /*
  41.  *  Use DeclareTrace() to declare trace variables.  Multiple modules may
  42.  *  declare trace variables of the same name.  These identifiers will be
  43.  *  passed to various trace routines later on.
  44.  */
  45.  
  46. DeclareTrace(Alpha);
  47. DeclareTrace(Beta);
  48. DeclareTrace(Gamma);
  49. DeclareTrace(Delta);
  50.  
  51. /*
  52.  *  Use DeclareTraceParents() to give a trace variable up to two parent
  53.  *  variables.  The parent names need not be declared with DeclareTrace().
  54.  *
  55.  *  Turning tracing on for a given parent automatically turns it on for
  56.  *  its children.
  57.  */
  58.  
  59. DeclareTraceParents(Epsilon, Root, P1);
  60. DeclareTraceParents(Indi,    Root, P1);
  61. DeclareTraceParents(Bravo,   Root, P2);
  62. DeclareTraceParents(Hotel,   Root, P2);
  63.  
  64. /*
  65.  *  Locally only one level hierarchy is allowed.  Better to modify
  66.  *  trace.c and add your own global hierarchy.
  67.  * 
  68.  *  XXX
  69.  */
  70.  
  71. DeclareTrace(P);
  72. DeclareTraceParent(P1, P);
  73. DeclareTraceParent(P2, P);
  74.  
  75. static int TestAssert;
  76.  
  77. main()
  78. {
  79.     if (getenv("TESTASSERT") != NULL)
  80.     TestAssert = atoi(getenv("TESTASSERT"));
  81.  
  82.     EnableTrace(true);
  83.  
  84.     if (getenv("TRACEFILE") == NULL)
  85.     TraceFileName("/dev/tty");
  86.  
  87.     puts("Trace Enabled");
  88.  
  89.     if (Traced(Alpha))
  90.     puts("Alpha is Traced");
  91.     TraceMsg0(Alpha, "Alpha Msg");
  92.     TraceMsg0(Beta,  "Beta Msg");
  93.     TraceMsg0(Gamma, "Gamma Msg");
  94.     TraceMsg0(Delta, "Delta Msg");
  95.  
  96.     TraceMsg0(Epsilon, "Epsilon Msg");
  97.     TraceMsg0(Indi,  "Indi Msg");
  98.     TraceMsg0(Bravo, "Bravo Msg");
  99.     TraceMsg0(Hotel, "Hotel Msg");
  100.  
  101.     TraceMsg0(P1, "P1 Msg");
  102.     TraceMsg0(P2, "P2 Msg");
  103.  
  104.     TraceMsg0(P, "P Msg");
  105.  
  106.     puts("");
  107.     puts("NOW TESTING SUBROUTINE TRACE.  FOUR LEVELS OF SUBROUTINES");
  108.     puts("NOTE: If 'TESTASSERT' enviroment variable is set to some");
  109.     puts("      number 1..4 I will call Assert(0) from inside that");
  110.     puts("      particular level");
  111.     fflush(stdout);
  112.     sleep(1);
  113.  
  114.     LevelAlpha(0);
  115.     LevelAlpha(1);
  116.  
  117.     puts("END OF TESTING SUBROUTINE TRACE");
  118.     fflush(stdout);
  119.  
  120.     EnableTrace(false);
  121.     puts("Trace Disabled");
  122. }
  123.  
  124. /*
  125.  *  Notes on subroutine call stack tracing.  You must specify the same
  126.  *  trace variable in the Pop as you did for the Push.
  127.  *
  128.  *  You may reuse trace block names (i.e. they could all use 'Alpha'
  129.  *  if they wanted to).  The trace block is used to determine when to
  130.  *  display subroutine tracing messages (if tracing is on for that 
  131.  *  block) and to retrieve the module name when printing messages.
  132.  */
  133.  
  134. LevelAlpha(x)
  135. {
  136.     TracePush(Alpha, "Level 1");
  137.     LevelBeta(0);
  138.     LevelBeta(1);
  139.     if (x && TestAssert == 1)
  140.     Assert(0);
  141.     TracePop(Alpha);
  142. }
  143.  
  144. LevelBeta(x)
  145. {
  146.     TracePush(Beta, "Level 2");
  147.     LevelGamma(0);
  148.     sleep(1);
  149.     LevelGamma(1);
  150.     sleep(1);
  151.     if (x && TestAssert == 2)
  152.     Assert(0);
  153.     TracePop(Beta);
  154. }
  155.  
  156. LevelGamma(x)
  157. {
  158.     TracePush(Gamma, "Level 3");
  159.     LevelDelta(0);
  160.     LevelDelta(1);
  161.     if (x && TestAssert == 3)
  162.     Assert(0);
  163.     TracePop(Gamma);
  164. }
  165.  
  166. LevelDelta(x)
  167. {
  168.     TracePush(Delta, "Level 4");
  169.     if (x && TestAssert == 4)
  170.     Assert(0);
  171.     TracePop(Delta);
  172. }
  173.  
  174.